package com.seagull.sb.dao.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.seagull.sb.dao.StudentDAO;
import com.seagull.sb.domain.Student;
import com.seagull.sb.exception.BaseException;
import com.seagull.sb.exception.SBError;
import com.seagull.sb.util.SBErrorConstant;
import com.seagull.sb.util.SBRequestContext;

public class StudentDAOImpl extends BaseDAO implements StudentDAO {
	

	@Override
	public List<Student> getStudents(SBRequestContext rc) throws Exception {
		Map<String,Object> qmap = new HashMap<String, Object>();
		
		qmap.put("org_id", new Long(rc.getOrgId()));
		if(rc.getClassId()>0){
			qmap.put("class_id", new Long(rc.getClassId()));
		}
		Object result = sqlMap.queryForList("studentDB.getStudents",qmap);
		if(result instanceof List){
			return (List<Student>)result;
		}
		return Collections.EMPTY_LIST;
	}
	
	@Override 
	public Student getStudentByToken(SBRequestContext rc, String id, String token)throws BaseException {
		
		Map<String,Object> qmap = new HashMap<String, Object>();
		Student student;
		qmap.put("org_id", new Long(rc.getOrgId()));
		if(rc.getClassId()>0){
			qmap.put("class_id", new Long(rc.getClassId()));
		}
		try{
			Object result = sqlMap.queryForObject("studentDB.getStudents",token);
			if(result instanceof Student){
				student = (Student) result;
				if(!student.isActive())
					throw new BaseException(new SBError(SBErrorConstant.AUTH_IS_NOT_ACTIVE, "Student is not in active."));
				else
					return (Student)result;
			}
		}catch(Exception e){
			throw new BaseException(new SBError(SBErrorConstant.AUTH_STUDENT_NOT_FOUND, "Student not found."));
		}
		throw new BaseException(new SBError(SBErrorConstant.AUTH_STUDENT_NOT_FOUND, "Student not found."));
	}

	@Override
	public List<Student> getStudents(SBRequestContext rc, String searchString) throws Exception {
		Map<String,Object> qmap = new HashMap<String, Object>();
		
		qmap.put("org_id", new Long(rc.getOrgId()));
		qmap.put("keyword1", searchString);
		qmap.put("keyword2", searchString);
		qmap.put("keyword3", searchString);
		qmap.put("keyword4", searchString);
		if(rc.getClassId()>0){
			qmap.put("class_id", new Long(rc.getClassId()));
		}
		Object result = sqlMap.queryForList("studentDB.searchStudentByKeyword",qmap);
		if(result instanceof List){
			return (List<Student>)result;
		}
		return Collections.EMPTY_LIST;
	}
	
	@Override
	public boolean saveStudent(SBRequestContext rc, Student student) throws Exception {
		boolean result = false;
		
		Map<String,Object> qmap = new HashMap<String, Object>();
		
		qmap.put("org_id", new Long(student.getOrgId()));
		qmap.put("class_id", new Long(student.getClassId()));
		qmap.put("first_name", student.getFname());
		qmap.put("last_name", student.getLname());
		qmap.put("sex", student.getGender());
		qmap.put("parent_name", student.getParentName());
		qmap.put("address", student.getAddress());
		qmap.put("phone", student.getPhone());
		qmap.put("mobile1", student.getMobilePrimary());
		qmap.put("mobile2", student.getMobileSecondary());
		qmap.put("blood_group", student.getBloodGroup());
		qmap.put("weight", student.getWeight());
		
		qmap.put("height", student.getHeight());
		qmap.put("dob", student.getDob());
		qmap.put("email", student.getEmail());
		
		Integer id = (Integer) sqlMap.insert("studentDB.saveStudent",qmap);
		result = true;

		
		return result;
	}
		
}
